Imports MIMS.WRS.Entities
Imports MIMS.WRS.Business

Public Class BOTerritoryEdit
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents ValidationSummary1 As System.Web.UI.WebControls.ValidationSummary
    Protected WithEvents lblMessage As System.Web.UI.WebControls.Label
    Protected WithEvents txtCurrency As System.Web.UI.WebControls.TextBox
    Protected WithEvents RequiredFieldValidator1 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents Requiredfieldvalidator2 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents ddlTerritory As System.Web.UI.WebControls.DropDownList
    Protected WithEvents ddlCountry As System.Web.UI.WebControls.DropDownList
    Protected WithEvents txtHD40ContFor15Prices As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtHD20ContFor15Prices As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtDate As System.Web.UI.WebControls.TextBox

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Function FindTerritory(ByVal territoryID As Integer, ByVal territories As TerritoryCollection) As Territory
        For Each objTerritory As Territory In territories
            If (objTerritory.TerritoryID = territoryID) Then
                Return objTerritory
            End If
        Next

        Return Nothing
    End Function

    Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
        MyBase.OnInit(e)
        AddHandler ddlCountry.SelectedIndexChanged, AddressOf CountryChanged
    End Sub

    Private Sub CountryChanged(ByVal sender As Object, ByVal e As EventArgs)
        If (ddlCountry.SelectedIndex <> -1 Or ddlCountry.SelectedIndex <> 0) Then
            Dim availTerritories As TerritoryCollection = TerritoryService.GetTerritoriesByCountry(Convert.ToInt32(ddlCountry.SelectedValue))
            ddlTerritory.DataSource = availTerritories
            ddlTerritory.DataTextField = TerritoryColumns.Description.ToString()
            ddlTerritory.DataValueField = TerritoryColumns.TerritoryID.ToString()
            ddlTerritory.DataBind()
            ddlTerritory.Items.Insert(0, New ListItem("---", Nulls.NullInteger.ToString()))
        End If
    End Sub

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Security.DenyIDSUser()

        If Not IsPostBack Then
            Dim productCode As String = ""
            Dim territoryID As Integer = 0
            If (Not Request.Item("id") Is Nothing) AndAlso (Request.Item("id").Length > 0) Then
                territoryID = Convert.ToInt32(Request.Item("id"))
            Else
                territoryID = RequestContext.TerritoryID
            End If
            If (Not Request.Item("secondValue") Is Nothing) AndAlso (Request.Item("secondValue").Length > 0) Then
                productCode = Request.Item("secondValue")
            Else
                productCode = RequestContext.ProductCode
            End If
            'Get HD - Territories Prices 1.5 information
            Dim hdTerritoriesPrices As hdTerritoriesPrices
            If (territoryID <> 0 AndAlso productCode <> "") Then
                hdTerritoriesPrices = BulkOrderTerritoryService.GetHDTerritoriesPricesByProductCodeAndTerritoryID(productCode, territoryID)
            End If

            If (Not hdTerritoriesPrices Is Nothing) Then
                Dim countries As New CountryCollection
                countries = CountryService.GetCountries(CountryColumns.CountryName, MyFramework.OrderDirection.ASC)
                ddlCountry.DataSource = countries
                ddlCountry.DataTextField = CountryColumns.CountryName.ToString()
                ddlCountry.DataValueField = CountryColumns.CountryID.ToString()
                ddlCountry.DataBind()

                Dim territory As territory = TerritoryService.GetTerritory(hdTerritoriesPrices.TerritoryID)
                ddlTerritory.Items.Insert(0, territory.Description)
                ddlCountry.SelectedValue = territory.CountryID.ToString

                If (ddlCountry.SelectedIndex <> -1 Or ddlCountry.SelectedIndex <> 0) Then
                    Dim availTerritories As TerritoryCollection = TerritoryService.GetTerritoriesByCountry(Convert.ToInt32(ddlCountry.SelectedValue))
                    ddlTerritory.DataSource = availTerritories
                    ddlTerritory.DataTextField = TerritoryColumns.Description.ToString()
                    ddlTerritory.DataValueField = TerritoryColumns.TerritoryID.ToString()
                    ddlTerritory.DataBind()

                    For Each item As territory In availTerritories
                        If (item.TerritoryID = territory.TerritoryID) Then
                            ddlTerritory.SelectedValue = item.TerritoryID.ToString
                            Exit For
                        End If
                    Next
                Else
                    ddlTerritory.Items.Insert(0, New ListItem("---", Nulls.NullInteger.ToString()))
                End If

                With hdTerritoriesPrices
                    If (.HomeDelivered40ContFor15Prices <> Nulls.NullDecimal) Then
                        txtHD40ContFor15Prices.Text = .HomeDelivered40ContFor15Prices.ToString
                    Else
                        txtHD40ContFor15Prices.Text = ""
                    End If

                    If (.HomeDelivered20ContFor15Prices <> Nulls.NullDecimal) Then
                        txtHD20ContFor15Prices.Text = .HomeDelivered20ContFor15Prices.ToString
                    Else
                        txtHD20ContFor15Prices.Text = ""
                    End If

                    txtCurrency.Text = .Currency
                    If (.Date <> Nulls.NullDate) Then
                        txtDate.Text = .Date.ToShortDateString
                    Else
                        txtDate.Text = ""
                    End If
                End With
            End If



            '        ddlTerritory.Items.Insert(0, New ListItem("---", Nulls.NullInteger.ToString()))
            'Dim objTerritory As Territory
            'For Each boTerritory As BulkOrderTerritory In boTerritories
            '    objTerritory = FindTerritory(boTerritory.TerritoryID, availTerritories)
            '    If Not (objTerritory Is Nothing) Then
            '        availTerritories.Remove(objTerritory)
            '    End If
            'Next
            'Dim countries As New CountryCollection
            'countries = CountryService.GetCountries(CountryColumns.CountryName, MyFramework.OrderDirection.ASC)
            'ddlCountry.DataSource = countries
            'ddlCountry.DataTextField = CountryColumns.CountryName.ToString()
            'ddlCountry.DataValueField = CountryColumns.CountryID.ToString()
            'ddlCountry.DataBind()
            'ddlCountry.Items.Insert(0, New ListItem("---", Nulls.NullInteger.ToString()))

            'If (Not boTerritory Is Nothing) Then
            '    For Each item As Country In countries
            '        If (item.CountryID = boTerritory.CountryID) Then
            '            ddlCountry.SelectedValue = item.CountryID.ToString
            '            Exit For
            '        End If
            '    Next
            'End If

            'If (ddlCountry.SelectedIndex <> -1 Or ddlCountry.SelectedIndex <> 0) Then
            '    Dim availTerritories As TerritoryCollection = TerritoryService.GetTerritoriesByCountry(Convert.ToInt32(ddlCountry.SelectedValue))
            '    ddlTerritory.DataSource = availTerritories
            '    ddlTerritory.DataTextField = TerritoryColumns.Description.ToString()
            '    ddlTerritory.DataValueField = TerritoryColumns.TerritoryID.ToString()
            '    ddlTerritory.DataBind()
            '    ddlTerritory.Items.Insert(0, New ListItem("---", Nulls.NullInteger.ToString()))

            '    For Each item As Territory In availTerritories
            '        If (item.TerritoryID = boTerritory.TerritoryID) Then
            '            ddlTerritory.SelectedValue = item.TerritoryID.ToString
            '            Exit For
            '        End If
            '    Next

            'Else
            '    ddlTerritory.Items.Insert(0, New ListItem("---", Nulls.NullInteger.ToString()))
            'End If
        End If
    End Sub

    Private Sub SaveTerritoryDetails()
        'Dim boTerritory As BulkOrderTerritory = BulkOrderTerritoryService.GetBulkOrderTerritoryByBulkOrderID(RequestContext.BulkOrderID)
        'Dim blnAddnew As Boolean

        'If (boTerritory Is Nothing OrElse (boTerritory.BulkOrderID = Nulls.NullInteger)) Then
        '    blnAddnew = True
        '    boTerritory = New BulkOrderTerritory
        'End If
        'boTerritory.BulkOrderID = RequestContext.BulkOrderID
        'boTerritory.CountryID = Convert.ToInt32(ddlCountry.SelectedItem.Value)
        'boTerritory.TerritoryID = Convert.ToInt32(ddlTerritory.SelectedItem.Value)

        'If (blnAddnew) Then
        '    BulkOrderTerritoryService.CreateBulkOrderTerritory(boTerritory)
        'Else
        '    BulkOrderTerritoryService.UpdateBulkOrderTerritory(boTerritory)
        'End If

    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If (Page.IsValid) Then
            SaveTerritoryDetails()
            lblMessage.Text = "Save successfully!"
        End If

    End Sub

    Private Sub btnSaveAndClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If (Page.IsValid) Then
            SaveTerritoryDetails()
            Response.Write("<script>window.close()</script>")
        End If
    End Sub


End Class
